package com.zz.hhbl.ui.translate

import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import com.google.android.material.shape.ShapePath
import com.google.android.material.shape.TriangleEdgeTreatment
import com.ss.bytertc.engine.type.MediaStreamType
import com.zz.framework.core.event.FinishAiEvent
import com.zz.framework.core.ext.dp
import com.zz.framework.core.ext.gone
import com.zz.framework.core.ext.onClick
import com.zz.framework.core.ext.onDownOrUpEvent
import com.zz.framework.core.ext.postDelayed
import com.zz.framework.core.ext.scrollToPositionWithOffset
import com.zz.framework.core.ext.toLanguage
import com.zz.framework.core.ext.viewBinding
import com.zz.framework.core.utils.language.Strings
import com.zz.hhbl.BuildConfig
import com.zz.hhbl.config.GlobalCache
import com.zz.hhbl.databinding.ActivityAiBinding
import com.zz.hhbl.databinding.ActivityTranslateBinding
import com.zz.hhbl.entity.AiSubtitleEntity
import com.zz.hhbl.ui.ai.BaseAiActivity
import com.zz.hhbl.ui.ai.MessageAdapter
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.Subscribe


/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/3/4 13:57.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
@AndroidEntryPoint
class TranslateActivity : BaseAiActivity() {
    val binding by viewBinding<ActivityTranslateBinding>()
    private lateinit var mAdapter: TranslateAdapter
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        binding.apply {
            showTopPop()
            recyclerView.layoutManager = LinearLayoutManager(this@TranslateActivity)
            recyclerView.adapter = TranslateAdapter().also {
                mAdapter = it
            }.setItemClickListener {
                it.text?.let { text ->
                    updateVoiceChat(handleType = 2, playText = text)
                }
            }
            //禁麦
            rtcRoom?.unpublishStream(MediaStreamType.RTC_MEDIA_STREAM_TYPE_AUDIO)
            ivHoldToSpeak.onDownOrUpEvent {
                tvTipPop.gone()
                if (it) {
                    //开麦
                    rtcRoom?.publishStream(MediaStreamType.RTC_MEDIA_STREAM_TYPE_AUDIO)
                    startVoiceWaveView()
                } else {
                    //禁麦
                    rtcRoom?.unpublishStream(MediaStreamType.RTC_MEDIA_STREAM_TYPE_AUDIO)
                    stopVoiceWaveView()
                }
            }
            btnBack.onClick {
                leaveRoom()
                stopVoiceChat()
                finish()
            }
        }
    }



    private fun showTopPop() {
        val shapeModel = ShapeAppearanceModel.builder()
            .setAllCornerSizes(6f.dp())
            .setBottomEdge(
                object : TriangleEdgeTreatment(8f.dp(), false) {
                    override fun getEdgePath(
                        length: Float,
                        center: Float,
                        interpolation: Float,
                        shapePath: ShapePath
                    ) {
                        super.getEdgePath(length, center, interpolation, shapePath)
                    }
                }
            )
            .build()
        val backgroundDrawable = MaterialShapeDrawable(shapeModel).apply {
            setTint(Color.parseColor("#3D83FF"))
            paintStyle = Paint.Style.FILL
        }
        binding.tvTipPop.setText(Strings.按住说话.toLanguage())
        binding.tvTipPop.background = backgroundDrawable
    }


    fun startVoiceWaveView(){
        binding.voiceWaveViewLeft.apply {
            lineWidth = 5f.dp()
            lineSpace = 5f.dp()
            addBody(27)
            addBody(17)
            addBody(38)
            addBody(91)
            addBody(38)
            addBody(24)
            addBody(8)
            addBody(60)
            start()
        }
        binding.voiceWaveViewRight.apply {
            lineWidth = 5f.dp()
            lineSpace = 5f.dp()
            addBody(27)
            addBody(17)
            addBody(38)
            addBody(91)
            addBody(38)
            addBody(24)
            addBody(8)
            addBody(60)
            start()
        }
    }
    fun stopVoiceWaveView(){
        binding.voiceWaveViewLeft.clear()
        binding.voiceWaveViewRight.clear()
    }

    override fun getAiType(): String {
        return "translateAI"
    }

    override fun getToneType(): String {
        return "zh_female_qingxinnvsheng_mars_bigtts"
    }

    override fun onMessageReceived(message: AiSubtitleEntity) {
        //去除空的
        if (message.text.isNullOrEmpty()) return
        mAdapter.addData(message)
        binding.recyclerView.scrollToPositionWithOffset(mAdapter.datas.size - 1)
    }

    @Subscribe
    fun onMessageReceived(message: FinishAiEvent) {
        leaveRoom()
        stopVoiceChat()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

}